#!/usr/bin/python

"""
Structures are cellfields that perform specific functions.
"""

import sys

from utils import *
from low_level import Cellfield
from logger import logger

class StructureError(Exception):
    """
    handling class for structure exceptions
    """
    
    def __init__(self, msg):
        """
        initialize the class
        """
        logger.error("structure: %s" % msg)

class Structure(Cellfield):
    """
    a structure of cells that perform a particular function
    
    TODO merge Structure and Module classes
    """

    def __init__(self, dimension =  STRUCT_DIMENSION, proportion = 1, type = 'X'):
        """
        initialize the structure
        """
        Cellfield.__init__(self, dimension, proportion, type)
        self.inputs = []
        self.result = ''
        self.parents = []
        self.children = []
        self.area = dimension * dimension / proportion
    
    def get_inputs(self):
        """
        return the current value of the input
        """
        return self.inputs
        
    def clear_inputs(self):
        """
        clear the list of inputs, in preparation for new ones
        """
        self.inputs = []
               
    def get_result(self):
        """
        get the output value computed by the structure
        """
        return self.result
        
    def add_child(self, childname):
        """
        add the name of a child to the list of children
        """
        self.children.append(childname)

    def add_input(self, operand):
        """
        set the input operands of a structure
        """
        self.inputs.append(operand)

    def compute(self):
        """
        use the input to generate an output
        """
        #TODO write inputs to cells
        #TODO perform MAC operation on each cell
        #TODO read out result
